home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Windows 95 API Bible
/
Windows 95 API Bible 3 Disc Set.iso
/
Win32 API Bible Book 1 of 3.iso
/
chapte25
/
ex1.c
next >
Wrap
C/C++ Source or Header
|
1995-04-20
|
3KB
|
85 lines
#include <genstub.c>
// Child thread just loops until program exits.
DWORD WINAPI ChildThreadProc( LPVOID lpNoData )
{
MSG msg;
HWND hWnd = CreateWindow( lpszAppName, "Child Thread's Window",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0,
CW_USEDEFAULT, 0, NULL, NULL, hInst, NULL );
if ( !hWnd )
return( FALSE );
ShowWindow( hWnd, SW_SHOW );
UpdateWindow( hWnd );
while (GetMessage( &msg, NULL, 0, 0 ))
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
ExitThread( TRUE );
}
// Windows message procedure for windows of both threads.
LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
static DWORD dwChildThreadId = 0; // child thread ID
static DWORD dwParentThreadId = 0; // parent thread ID
static HWND hChildThreadWnd = 0; // child thread's window
static HWND hParentThreadWnd = 0; // parent thread's window
static BOOL fAttachedInput = FALSE; // status of attached input
switch (uMsg)
{
case WM_CREATE: // Set globals and create thread.
if (hChildThread==0)
{
hParentThreadWnd = hWnd;
dwParentThreadId = GetCurrentThreadId();
// Create a thread
CreateThread( NULL, 0, ChildThreadProc,
NULL, 0, &dwChildThreadId );
}
else
hChildThreadWnd = hWnd;
return DefWindowProc( hWnd, uMsg, wParam, lParam );
case WM_COMMAND: // process menu items
switch ( LOWORD( wParam ) )
{
case IDM_TEST:
{
HDC hDC = GetDC( hWnd );
// Toggle attached thread input.
if ( fAttachedInput )
AttachThreadInput( dwChildThreadId,
dwParentThreadId, !fAttachedInput );
else
AttachThreadInput( dwChildThreadId,
dwParentThreadId, !fAttachedInput );
fAttachedInput = !fAttachedInput;
// Print Message.
TextOut( hDC, 0, 0, fAttachedInput ?
"Attached " : "Detached ", 10);
ReleaseDC( hWnd, hDC );
// Set active window to opposite window.
// Only succeeds if inputs are attached.
if ( hWnd==hParentThreadWnd )
SetActiveWindow( hChildThreadWnd );
else // Child thread's window
SetActiveWindow( hParentThreadWnd );
}
break;
case IDM_EXIT:
DestroyWindow( hWnd );
break;
}
break;
case WM_DESTROY: // only parent closes
PostQuitMessage( 0 );
break;
default:
return DefWindowProc( hWnd, uMsg, wParam, lParam );
}
return (NULL);
}